package cn.kgm.prototype;

import cn.kgm.prototype.shape.Circle;
import cn.kgm.prototype.shape.Rectangle;
import cn.kgm.prototype.shape.Square;

import java.util.Hashtable;
import java.util.Objects;

public class ShapeCache {

    private static Hashtable<String, Shape> shapeTable = new Hashtable<String, Shape>();

    public static Shape getShape(String shapeId) {
        Shape shape = shapeTable.get(shapeId);
        if (Objects.nonNull(shape)) return (Shape) shape.clone();
        return null;
    }

    // 对每种形状都运行数据库查询，并创建该形状
    // shapeTable.put(shapeKey, shape);
    // 例如，我们要添加三种形状
    public static void loadCache() {
        Circle circle = new Circle();
        circle.setId("1");
        shapeTable.put(circle.getId(), circle);

        Square square = new Square();
        square.setId("2");
        shapeTable.put(square.getId(), square);

        Rectangle rectangle = new Rectangle();
        rectangle.setId("3");
        shapeTable.put(rectangle.getId(), rectangle);
    }

    public Hashtable<String, Shape> getShapeTable() {
        return shapeTable;
    }

    public void setShapeTable(Hashtable<String, Shape> shapeTable) {
        this.shapeTable = shapeTable;
    }
}
